package cake.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cake.bean.OrderDetail;
import cake.db.MySqlDb;
//������ϸ��ݿ���ʶ���
public class OrderDetailDao {
private MySqlDb db=new MySqlDb();
	//ͨ��id��ȡ������ϸ����
	public OrderDetail getOrderDetail(long id)
	{
		OrderDetail detail=null;
		String sql="select * from detail where id="+id;
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				detail=new OrderDetail();
				detail.setCakeId(rs.getLong("cakeId"));
				detail.setId(rs.getLong("id"));
				detail.setOrderId(rs.getLong("orderId"));
				detail.setQuantity(rs.getInt("quantity"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			db.Close();
		}
		
		return detail;
	}
	//��ҳ��ȡ������ϸ�б?��pageSizeΪ0ʱ��ʾ����ҳ
	public List<OrderDetail> getOrderDetails(int currPage,int pageSize,String where)
	{
		List<OrderDetail> details=new ArrayList<OrderDetail>();
		String sql="select * from detail ";
		if(!where.equals(""))
		{
			sql+=" Where "+where;
		}
		if(pageSize!=0)
		{
			sql+=" limit "+((currPage-1)*pageSize)+","+pageSize;
		}
		
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				OrderDetail detail=new OrderDetail();
				detail.setCakeId(rs.getLong("cakeId"));
				detail.setId(rs.getLong("id"));
				detail.setOrderId(rs.getLong("orderId"));
				detail.setQuantity(rs.getInt("quantity"));
				
				details.add(detail);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		
		return details;
	}
	//���Ӷ�����ϸ
	public boolean addOrderDetail(OrderDetail detail)
	{
		String sql="Insert into OrderDetail(cakeId,orderId,quantity) values("+detail.getCakeId()+","+detail.getOrderId()+","+detail.getQuantity()+")";
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
		
	}
	//�޸Ķ�����ϸ
	public boolean updateOrderDetail(OrderDetail detail)
	{
		OrderDetail oldOrderDetail=getOrderDetail(detail.getId());
		
		if(oldOrderDetail==null)
		{
			return false;
		}
		
		String sql="Update OrderDetail set cakeId="+detail.getCakeId()+",orderId="+detail.getOrderId()+",quantity="+detail.getQuantity()+" where id="+detail.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
	//ɾ����ϸ
	public  boolean deleteOrderDetail(OrderDetail detail)
	{
		OrderDetail oldOrderDetail=getOrderDetail(detail.getId());
		
		if(oldOrderDetail==null)
		{
			return false;
		}
		
		String sql="Delete from OrderDetail where id="+detail.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
}
